import axiosInstance from "./axiosInstance";

// 路径为:'@/apis/orderService'
// 订单相关 API

// 创建订单
export const createOrder = async (userId, orderItems) => {
  try {
    const response = await axiosInstance.post('/orders', { userId, orderItems });
    return response.data;
  } catch (error) {
    console.error('创建订单失败:', error);
    throw error;
  }
};

// 获取用户订单
export const getUserOrders = async (userId) => {
  try {
    const response = await axiosInstance.get(`/orders/user/${userId}`);
    return response.data;
  } catch (error) {
    console.error('获取用户订单失败:', error);
    throw error;
  }
};

// 获取特定订单
export const getOrderById = async (orderId) => {
  try {
    const response = await axiosInstance.get(`/orders/${orderId}`);
    return response.data;
  } catch (error) {
    console.error('获取订单详情失败:', error);
    throw error;
  }
};

// 更新订单状态
export const updateOrderStatus = async (orderId, newStatus) => {
  try {
    const response = await axiosInstance.put(`/orders/${orderId}/status`, { newStatus });
    return response.data;
  } catch (error) {
    console.error('更新订单状态失败:', error);
    throw error;
  }
};

// 取消订单
export const cancelOrder = async (orderId) => {
  try {
    const response = await axiosInstance.post(`/orders/${orderId}/cancel`);
    return response.data;
  } catch (error) {
    console.error('取消订单失败:', error);
    throw error;
  }
};

//删除订单
export const deleteOrder = async (orderId) => {
  try {
    const response = await axiosInstance.delete(`/orders/${orderId}`);
    return response.data;
  } catch (error) {
    console.error('删除订单失败:', error);
    throw error;
  }
};

// 获取用户订单统计
export const getOrderStatistics = async (userId) => {
  try {
    const response = await axiosInstance.get(`/orders/statistics/${userId}`);
    return response.data;
  } catch (error) {
    console.error('获取订单统计失败:', error);
    throw error;
  }
};